package com.jl15988.simpletools.appcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jl15988.simpletools.appcore.dto.AuditDTO;
import com.jl15988.simpletools.appcore.dto.AuditQueryDTO;
import com.jl15988.simpletools.appcore.dto.AuditRequestDTO;
import com.jl15988.simpletools.appcore.service.AuditService;
import com.jl15988.simpletools.common.annotation.Log;
import com.jl15988.simpletools.common.constant.OperationType;
import com.jl15988.simpletools.common.utils.Result;
import com.jl15988.simpletools.common.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 审核控制器
 *
 * @author jl15988
 */
@RestController
@RequestMapping("/api/audits")
public class AuditController {

    @Autowired
    private AuditService auditService;

    /**
     * 分页查询审核列表
     *
     * @param query 查询条件
     * @return 分页结果
     */
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<IPage<AuditDTO>> getAuditPage(AuditQueryDTO query) {
        return Result.success(auditService.getAuditPage(query));
    }

    /**
     * 获取应用最新的审核记录
     *
     * @param appId 应用ID
     * @return 审核记录
     */
    @GetMapping("/latest/{appId}")
    public Result<AuditDTO> getLatestAudit(@PathVariable Long appId) {
        return Result.success(auditService.getLatestAudit(appId));
    }

    /**
     * 提交审核
     *
     * @param request 审核请求DTO
     * @param httpServletRequest HTTP请求
     * @return 审核ID
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "审核应用", operationType = OperationType.UPDATE)
    public Result<Long> submitAudit(@Valid @RequestBody AuditRequestDTO request, HttpServletRequest httpServletRequest) {
        Long userId = TokenUtils.getUserId(httpServletRequest);
        String username = TokenUtils.getUsername(httpServletRequest);
        return Result.success(auditService.submitAudit(request, userId, username));
    }

    /**
     * 获取待审核应用数量
     *
     * @return 待审核应用数量
     */
    @GetMapping("/pending/count")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Long> getPendingAuditCount() {
        return Result.success(auditService.getPendingAuditCount());
    }

    /**
     * 获取开发者待审核应用数量
     *
     * @param httpServletRequest HTTP请求
     * @return 待审核应用数量
     */
    @GetMapping("/developer/pending/count")
    @PreAuthorize("hasAnyRole('DEVELOPER', 'ADMIN')")
    public Result<Long> getDeveloperPendingAuditCount(HttpServletRequest httpServletRequest) {
        Long userId = TokenUtils.getUserId(httpServletRequest);
        return Result.success(auditService.getDeveloperPendingAuditCount(userId));
    }
} 